package com.anycc.cloud.core.controller;

import com.anycc.cloud.common.dto.datatable.DataTableRequest;
import com.anycc.cloud.common.dto.datatable.DataTableResponse;
import com.anycc.cloud.common.dto.easyui.Tree;
import com.anycc.cloud.common.dto.json.ListResponse;
import com.anycc.cloud.common.dto.json.Response;
import com.anycc.cloud.common.dto.json.SuccessResponse;
import com.anycc.cloud.common.dto.query.OrderBy;
import com.anycc.cloud.core.entity.Module;
import com.anycc.cloud.core.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * module account
 */
@RestController
@RequestMapping("module")
public class ModuleController {
    @Autowired
    private ModuleService moduleService;

    /**
     * create a new module
     *
     * @param module module
     * @return Response
     */
    @PostMapping("create")
    public Response create(Module module) {
        moduleService.create(module);
        return new SuccessResponse();
    }

    /**
     * build module tree
     *
     * @return tree
     */
    @GetMapping("tree")
    public Response tree() {
        return new ListResponse<>(moduleService.tree());
    }

    /**
     * datatable paging query
     *
     * @param module  Module
     * @param request DataTableRequest
     * @return DataTableResponse
     */
    @GetMapping("findByPaging")
    public DataTableResponse<Module> findByPaging(Module module, DataTableRequest request) {
        List<OrderBy> orders = new ArrayList<>();
        orders.add(new OrderBy("priority"));
        orders.add(new OrderBy("desc", "createTime"));
        return new DataTableResponse<Module>(this.moduleService.findByPaging(module, request.buildPageRequest(orders)), request);
    }

    /**
     * delete module by id
     *
     * @param id module id
     * @return response
     */
    @PostMapping("delete")
    public Response delete(@RequestParam("id") String id) {
        moduleService.delete(id);
        return new SuccessResponse();
    }

    /**
     * find module by id
     *
     * @param id id
     * @return Module
     */
    @GetMapping("findById")
    public Module findById(@RequestParam("id") String id) {
        return moduleService.findById(id);
    }

    /**
     * update module
     *
     * @param module module
     * @return response
     */
    @PostMapping("update")
    public Response update(Module module) {
        moduleService.update(module);
        return new SuccessResponse();
    }

    /**
     * build permission tree
     *
     * @return response
     */
    @GetMapping("tree/permission")
    public Response permissionTree() {
        return new ListResponse<Tree>(this.moduleService.permissionTree());
    }

    /**
     * build permission tree
     *
     * @param roleId role id
     * @return Response
     */
    @GetMapping("tree/permission/selected")
    public List<Tree> permissionTreeSelectedByRoleId(@RequestParam("id") String roleId) {
        return moduleService.permissionTreeSelectedByRoleId(roleId);
    }

}
